import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import org.jivesoftware.smack.XMPPException;


public class LoginPanel extends JPanel {
	protected JLabel namePrompt;
	protected JLabel passPrompt;
	protected JLabel servPrompt;
	protected JTextField nameField;
	protected JPasswordField passField;
	protected JComboBox servList;
	protected Color bColor = new Color(43, 43, 43);
	protected Color tColor = Color.white;
	protected JButton login, cancel, register;
	protected Output o;
	
	public LoginPanel(Output arg) {

		o = arg;
		
		
		this.setLayout(new BorderLayout());
		this.setBackground(bColor);
		
		JPanel jpn = new JPanel();
		jpn.setBackground(bColor);
		jpn.setBorder(BorderFactory.createEmptyBorder(10, 18, 10, 0));
		jpn.setLayout(new BorderLayout());
		this.add(jpn, BorderLayout.NORTH);
		
		JLabel jl = new JLabel("  GENERALS ");
		jl.setFont(new Font("Arial", Font.BOLD, 20));
		jl.setForeground(Color.white);
		jpn.add(jl, BorderLayout.NORTH);

		
		JPanel jp = new JPanel();
		jp.setLayout(new BoxLayout(jp, BoxLayout.PAGE_AXIS));
		this.add(jp, BorderLayout.CENTER);

		JPanel jpt = new JPanel();
		jpt.setLayout(new BorderLayout());
		jpt.setBackground(tColor);
		jpt.setBorder(BorderFactory.createMatteBorder(15, 15, 0, 15, tColor));
		jp.add(jpt);

		namePrompt = new JLabel("Username:       ");
		jpt.add(namePrompt, BorderLayout.WEST);
		nameField = new JTextField();
		nameField.setPreferredSize(new Dimension(100, 10));
		jpt.add(nameField, BorderLayout.CENTER);
	

		JPanel jpb = new JPanel();
		jpb.setLayout(new BorderLayout());
		jpb.setBackground(tColor);
		jpb.setBorder(BorderFactory.createLineBorder(tColor, 15));
		passPrompt = new JLabel("Password:       ");
		jpb.add(passPrompt, BorderLayout.WEST);
		passField = new JPasswordField();
		passField.setPreferredSize(new Dimension(100, 10));
		jpb.add(passField, BorderLayout.CENTER);
		jp.add(jpb);


		JPanel jpl = new JPanel();
		jpl.setBackground(bColor);
		jpl.setBorder(BorderFactory.createEmptyBorder(5, 0, 5, 5));
		jpl.setLayout(new BorderLayout());
		this.add(jpl, BorderLayout.SOUTH);
		
		
		JPanel buttonPanel = new JPanel();
		buttonPanel.setLayout(new FlowLayout());
		buttonPanel.setBackground(bColor);
		jpl.add(buttonPanel, BorderLayout.EAST);
		
		login = new JButton("Login");
		login.setPreferredSize(new Dimension(85, 25));
		login.addActionListener(new LoginListener());
		buttonPanel.add(login);
		register = new JButton("Register");
		register.setPreferredSize(new Dimension(85, 25));
		register.addActionListener(new RegisterListener());
		buttonPanel.add(register);
		cancel = new JButton("Cancel");
		cancel.setPreferredSize(new Dimension(85, 25));
		cancel.addActionListener(new CancelListener());
		buttonPanel.add(cancel);
		
		//testing values
		this.nameField.setText("turtle");
		this.passField.setText("mrbobo");
		
		nameField.addKeyListener(new AreaKeyListener());
		passField.addKeyListener(new AreaKeyListener());
	}
	private class AreaKeyListener implements KeyListener {
		public void keyTyped(KeyEvent e) {
		}
		public void keyPressed(KeyEvent e) {
			if (e.getKeyCode() == KeyEvent.VK_ENTER) {
			}
		}
		public void keyReleased(KeyEvent e) {
			if (e.getKeyCode() == KeyEvent.VK_ENTER)
				o.login(nameField.getText(), passField.getText());
		}
	}
	
	
	private class LoginListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			o.login(nameField.getText(), passField.getText());
		}
	}
	
	private class CancelListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			System.exit(0);
		}
	}
	
	private class RegisterListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			JFrame frame = new JFrame();
			frame.setTitle("Login");
			frame.setLocationRelativeTo(null);
			frame.setSize(440, 300);
			frame.setContentPane(new RegisterPanel(o));
			frame.setVisible(true);
			o.loginFrame.setVisible(false);
			o.registerFrame = frame;
			
		}
	}
}
